﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.Data;
using HitachiLib.Utils;
using HitachiApp.Model.Manage;
using HitachiApp.Model.Projects;
using HitachiApp.Model.Resources;

namespace HitachiApp.Model
{
    public class MainPageModel : INotifyPropertyChanged
    {
        private ScheduleTabViewModel schedule_tabview_model = new ScheduleTabViewModel();
        private ManageViewModel manage_view_model;
        private ProjectManageViewModel project_manage_view_model;
        private ResourceManageViewModel resource_manage_view_model;
        private Resource login_account;

        public MainPageModel()
        {
            manage_view_model = new ManageViewModel(this);
            project_manage_view_model = new ProjectManageViewModel();
            resource_manage_view_model = new ResourceManageViewModel();
            WCFClient.InitMainPage(this);
        }

        public ScheduleTabViewModel ScheduleTabViewModel
        {
            get { return schedule_tabview_model; }
        }
        public ManageViewModel ManageTabViewModel
        {
            get { return manage_view_model; }
        }
        public ProjectManageViewModel ProjectTabViewModel
        {
            get { return project_manage_view_model; }
        }
        public ResourceManageViewModel ResourceTabViewModel
        {
            get { return resource_manage_view_model; }
        }

        public Resource LoginAccount
        {
            get { return login_account; }
            set {
                login_account = value;
                manage_view_model.Load(login_account);
            }
        }

        public Boolean UnLocked
        {
            get { return !WCFClient.IsInUse(); }
        }
        public void LockChanged()
        {
            RaisePropertyChanged("UnLocked");
        }

        public bool ManageViewModel_Loaded
        {
            set
            {
                if (value)
                    schedule_tabview_model.SetLoginAccount(login_account);
            }
        }

        public bool Logined
        {
            get { return login_account != null; }
        }
        public bool Logouted
        {
            get { return login_account == null; }
        }

        public bool ScheduleViewAccess
        {
            get
            {
                if (LoginAccount != null)
                {
                    return (LoginAccount.Data.Access & Resource.ScheduleAccess) > 0;
                }
                return false;
            }
        }

        public bool ManageViewAccess
        {
            get
            {
                if (LoginAccount != null)
                {
                    return (LoginAccount.Data.Access & Resource.ManageAccess) > 0;
                }
                return false;
            }
        }

        public void RefreshAccount()
        {
            RaisePropertyChanged("Logined");
            RaisePropertyChanged("Logouted");
            RaisePropertyChanged("LoginAccount");
            RaisePropertyChanged("ScheduleViewAccess");
            RaisePropertyChanged("ManageViewAccess");
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
